<template>
	<view class="">
		<heade title="下单" :fanStatus="false" bg_color="bg_fff"></heade>
		<view class="">
			<!-- <view class="bg_fff p-t10 p-b15">
				<view class="headeClass  co_fff f-s20 f-w600 d-flex align-center justify-content-center">您本月还未产生销售，加油哟！</view>
			</view> -->
			<view class="">
				<!-- 常用功能 -->
				<view class="bg_fff p-b20  box_box m-t12 m-l12 m-r12 border-r10">
					<view class="f-s15 p-t20 p-l15">常用功能</view>
					<view class="d-flex align-center m-t20">
						<view class="text-center" style="width: 33.33%;" v-for="(item,i) in commonly" @tap="$common.url(item.url)">
							<text :class="[item.icon,i == 0?'f-s28':'f-s22']" class="iconfont " :style="{'color': item.color}"></text>
							<view class="f-s14 m-t8">{{item.name}}</view>
						</view>
					</view>
				</view>
				<!-- 全部列表 -->
				<view class="bg_fff p-b20  box_box m-t12 m-l12 m-r12 border-r10">
					<view class="f-s15 p-t20 p-l15">全部列表</view>
					<view class="d-flex align-center m-t20">
						<view class="text-center" style="width: 33.33%;" v-for="(item,i) in wholeList" @tap="$common.url(item.url)">
							<text :class="[item.icon,i == 2?'f-s22':'f-s28']" class="iconfont " :style="{'color': item.color}"></text>
							<view class="f-s14 m-t8">{{item.name}}</view>
						</view>
					</view>
				</view>
				<!-- 常用设置 -->
				<view class="bg_fff p-b20  box_box m-t12 m-l12 m-r12 border-r10">
					<view class="f-s15 p-t20 p-l15">常用设置</view>
					<view class="d-flex align-center m-t20">
						<view class="text-center" style="width: 33.33%;" v-for="(item,i) in setUpList" @tap="urlLink(item)">
							<text :class="[item.icon]" class="iconfont f-s24" :style="{'color': item.color}"></text>
							<view class="f-s14 m-t8">{{item.name}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		openSqlite,
		userInfoSQL,
		addUserInformation,
	} from '@/common/sqlite.js';
	export default {
		data() {
			return {
				index:0,
				commonly:[
					{name:'销售订单',icon:'icon-xiaoshoudingdan',color:'#ff5c5f',url:'/pages/parameters/parameters?title=销售订单'},
					{name:'销货单',icon:'icon-xierushujuku',color:'#fe8f28',url:'/pages/parameters/parameters?title=销货单'},
					{name:'退货单',icon:'icon-tuihuodan',color:'#fe8f28',url:'/pages/parameters/parameters?title=退货单'},
				],
				wholeList:[
					{name:'销售订单列表',icon:'icon-dingdan',color:'#33b0fa',url:'/pages/salesOrderList/salesOrderList'},
					{name:'销货单列表',icon:'icon-dingdanguanli',color:'#33b0fa',url:'/pages/salesOrderList/salesList'},
					{name:'草稿列表',icon:'icon-caogaoxiang',color:'#06b447',url:'/pages/aDraftList/aDraftList'},
				],
				setUpList:[
					{name:'设置',icon:'icon-shezhi',color:'#33b0fa',url:'/pages/home/setUp'},
					{name:'更新数据',icon:'icon-shujubiangeng',color:'#33b0fa'},
					// {name:'云打印设置',icon:'icon-yundayin',color:'#fd5e63'},
				],
				backButtonPress:0,
				InventoryPrice:[],
				InventoryUnit:[],
				InventoryQueryV2:[],
				CurrentStock:[],
				// #ifdef APP-PLUS
				number:1000,
				// #endif
				// #ifdef H5
				number:1000,
				// #endif
			}
		},
		onLoad() {
			var isOpen = plus.sqlite.isOpenDatabase({
				name: getApp().globalData.dbName, //数据库的名字  
				path: getApp().globalData.dbPath //地址  
			});
			// if(!this.$db.get('commodity') && this.$db.get('token')) {
			// 	this.getWarehouse()
			// }
			// this.getDepartment()
			// // #ifdef APP-PLUS
			// var page = this.$mp.page.$getAppWebview();
			// page.setStyle({ popGesture: 'none' });
			// // #endif 
		},
		onBackPress(options) {  
		// 	// #ifdef APP-PLUS
		// 　　let _this = this
		// 	uni.showModal({
		// 	title: '温馨提示',
		// 	content: '您确定要退出',
		// 		success: function (res) {
		// 			if (res.confirm) {
		// 				console.log('用户点击确定');
		// 				plus.runtime.quit();
		// 			} else if (res.cancel) {
		// 				console.log('用户点击取消');
		// 			}
		// 		}
		// 	});
		// 　　return true;
		// 	// #endif
		},
		methods: {
			urlLink(item) {
				if(item.name == '更新数据') {
					
					// this.getWarehouse()
					this.getDepartment()
				}else {
					this.$common.url(item.url)
				}
			},
			// 获取仓库列表
			getWarehouse() {
				uni.showLoading({
					title: '正在同步数据',
					mask:true 
				});
				this.$http.post('ApiAS/WarehouseQuery',{ "param": {}},true).then(res => {
					if(res.statusCode = 200) {
						
						this.$db.set('warehouse',JSON.parse(res.data))
						// this.getClassQuery()
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
			// 获取用户分类
			getClassQuery() {
				this.$http.post('ApiAS/PartnerClassQuery',{param:{}},true).then(res => {
					if(res.statusCode == 200) {
						this.$db.set('kehuList',res.data)
						// this.getorder()
						this.getUsersList()
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
			// 获取用户列表
			getUsersList() {
				let resData = {
					param:{
						"pageSize": Number(this.number + '0'),
						"pageIndex": 1,
						ts:''
					} 
				}
				this.$http.post('ApiAS/PartnerQueryV2',resData,true).then(res => {
					if(res.statusCode == 200) {
						this.$db.set('theCustomer',res.data)
						this.getorder()
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
			// 获取商品分类
			getorder() {
				this.$http.post('ApiAS/InventoryClassQuery',{"param": {}},true).then(res => {
					if(res.statusCode == 200) {
						this.$db.set('goodsList',res.data)
						this.getDepartment()
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
			// 获取商品现存量
			getDepartment() {
				uni.showLoading({
					title: '正在同步数据-111',
					mask:true 
				});
				this.$http.post('ApiST/CurrentStock',{param: {"pageSize": Number(this.number + '0')}},true).then(res => {
					if(res.statusCode == 200) {
						this.CurrentStock = res.data
						this.getData()
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
			// 获取商品列表
			getData() {
				let resData = {
					param:{
						"pageSize": this.number,
						"pageIndex": 1,
						ts:''
					}
				}
				this.$http.post('ApiAS/InventoryQueryV2',resData,true).then(res => {
					if(res.statusCode == 200) {
						this.InventoryQueryV2 = res.data
						this.getConfig()
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
			// 查询存货价格本
			getConfig(){
				this.$http.post('ApiAS/InventoryPrice',{ "ts": 0,"pageSize":Number(this.number + '0')},true).then(res => {
					if(res.statusCode == 200) {
						this.InventoryPrice = res.data
						// console.log(res.data[0])
						this.getOryUnit()
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
			// 查询存货计量单位
			getOryUnit(){
				let _this = this
				this.$http.post('ApiAS/InventoryUnit',{ "ts": 0,"pageSize": Number(this.number + '00')},true).then( res => {
					if(res.statusCode == 200) {
						let list = []
						this.InventoryUnit = res.data
						res.data.forEach((q,k) => {
							if(q.unitName == '中组') {
								res.data.splice(k,1)
							}
						})
						res.data.forEach((j,i) => {
							_this.InventoryPrice.forEach((t,index) => {
								if(j.invCode == t.invCode && j.unitID == t.idunit) {
									list.push({
										"id":t.id,
										"UnitCode":j.invCode || '',
										"UnitName": j.unitName || '',
										// "Price": t.invLSPrice || '',
										"RetailPriceNew":t.retailPriceNew || '',
										Price:t.retailPrice,
										PriceObj:t
									})
								}
							})
						})
						let list2 = []
						let value = []
						let Existing = []
						_this.InventoryQueryV2.forEach(e => {
							this.CurrentStock.forEach((o,index) => {
								if(e.code == o.inventoryCode) {
									Existing.push(o)
									e.ExistingQuantityList = Existing
									if(o.warehouseName == '主仓') {
										e.ExistingQuantity = o.existingQuantity
									}
								}else {
									Existing = []
								}
							})
							if(e.priuserdefdecm1) {
								value = e.priuserdefdecm1.split('.')
								e.priuserdefdecm1 = value[0]
							}
							list.forEach(k => {
								if(e.code == k.UnitCode) {
									list2.push(k)
									e.partnerUnitPrice = list2
									// console.log(e.partnerUnitPrice)
								}else {
									list2 = []
								}
							})
							e.isStatus = false;
							e.checked = false;
							e.number = 0;
							e.giftChecked = false;
							if(e.partnerUnitPrice) {
								if(e.partnerUnitPrice[0]) {
									e.partnerUnitPrice.forEach((j,index) => {
										j.count = false
									})
									e.RetailPriceNew = e.partnerUnitPrice[0].RetailPriceNew
									if(this.$db.get('default_config').unit == 2) {
										e.partnerUnitPrice[1].count = true
									}else {
										e.partnerUnitPrice[0].count = true
									}
									
									e.Price = e.partnerUnitPrice[0].Price
									e.UnitName = e.partnerUnitPrice[0].UnitName
								}
							}
						})
						console.log(_this.InventoryQueryV2)
						this.$db.set('commodity',_this.InventoryQueryV2)
						uni.hideLoading();
						this.$common.show('同步成功')
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
		}
	}
</script>

<style>
	page {
		background: #f3f2f7;
	}
</style>
<style lang="scss">
	.headeClass{
		width: 710rpx;
		height: 190rpx;
		background-color: #4095ff;
		border-radius: 20rpx;
		margin: 0 auto;
	}
</style>
